package cn.fresh.xuanyuan.module.member.controller.app.message;

import cn.fresh.xuanyuan.framework.common.pojo.CommonResult;
import cn.fresh.xuanyuan.framework.common.pojo.PageParam;
import cn.fresh.xuanyuan.framework.common.pojo.PageResult;
import cn.fresh.xuanyuan.framework.security.core.annotations.PreAuthenticated;
import cn.fresh.xuanyuan.module.member.dto.MessageDto;
import cn.fresh.xuanyuan.module.member.service.message.MessageService;
import cn.fresh.xuanyuan.module.member.vo.MessageCountVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.Map;

import static cn.fresh.xuanyuan.framework.common.pojo.CommonResult.error;
import static cn.fresh.xuanyuan.framework.common.pojo.CommonResult.success;
import static cn.fresh.xuanyuan.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 APP - 用户消息")
@RestController
@RequestMapping(value = "/member/msg")
@Slf4j
public class MessageController {

    @Resource
    private MessageService messageService;

    @GetMapping(value = "/getUnread")
    @PreAuthenticated
    public CommonResult<MessageCountVO> getUnread() {
        MessageCountVO countVO = messageService.getUnread(getLoginUserId());
        return success(countVO);
    }


    @GetMapping(value = "/getList")
    @PreAuthenticated
    public CommonResult<PageResult<MessageDto>> getList(@RequestParam(name = "msgType", required = false) Integer msgType, @ModelAttribute PageParam pageParam) {
        PageResult<MessageDto> page = messageService.getMsgList(getLoginUserId(), msgType, pageParam);
        return success(page);
    }

    @PostMapping(value = "/read")
    @PreAuthenticated
    public CommonResult<String> read(@RequestBody Map<String, String> map) {
        if(map == null || StringUtils.isBlank(map.get("id"))) {
            return error(400, "必传参数不能为空");
        }
        messageService.read(map.get("id"));
        return success("success");
    }
}
